<?php

namespace App\Services;

/**
 * 文件服务
 * Class FileService
 * @package App\Services
 */
class FileService extends BaseService
{
    /**
     * 上传图片（只上传到本地）
     */
    public function uploadImageToLocal()
    {
        $file = request()->file('image');

        if(!$file){
            return $this->ierror(-1,'图片不存在');
        }

//        $res = WechatService::contentSecurity($file->getPathname());
//        if($res['errcode'] == '87014'){
//            return $this->error(-1,'图片含有违法违规内容');
//        }

        $messages = [
            'required' => '请选择需要上传的文件!',
            'mimes' => '只能上传图片!',
            'max' => '超过太大!',
        ];

//        $validator = Validator::make(request()->all(), [
//            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:5120',
//        ], $messages);
//
//        if ($validator->fails()) {
//            //验证失败
//            $message = $validator->errors()->first();
//            return $this->error(-1, $message);
//        }

        $uniqid = uniqid();
        $imageName = $uniqid . '.' . $file->getClientOriginalExtension();

        $type = 'image';
        $setting['folder'] = "legwork/{$type}s";
        $setting['folder'] .= '/' . date('Y/m/');

        $filepath = $setting['folder'] . $imageName;
        $param['name'] = $imageName;
        $param['tmp_name'] = $file->getPathname();
        $res = file_upload($param, $type, $filepath, true);

        return $res;
    }

    /**
     * 上传至远程
     */
    public function uploadToRemote()
    {
        $result = $this->uploadImageToLocal();
        if(is_error($result)){
            return $this->error($result['errno'], $result['message']);
        }
        //图片上传到阿里云
        file_remote_upload($result['path']);

        return $this->success([
            'url' => wgTomedia($result['path'])
        ]);
    }
}